package org.ymjava.medical.wx.web;


import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.apache.ibatis.annotations.Param;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import org.ymjava.medical.core.util.ResponseUtil;
import org.ymjava.medical.db.AllPackage.biz.BankForUserBiz;
import org.ymjava.medical.db.AllPackage.biz.QuestionBankBiz;
import org.ymjava.medical.db.AllPackage.entity.BankForUser;
import org.ymjava.medical.db.AllPackage.entity.QuestionBank;

import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@RestController
@RequestMapping("/wx_answer/question_bank")
public class QuestionBankController {

    private final Log logger = LogFactory.getLog(QuestionBankController.class);

    @Autowired
    QuestionBankBiz questionBankBiz;

    @Autowired
    BankForUserBiz bankForUserBiz;

    @GetMapping("/bank_list")
    public Object bank_list(Integer userId){

        //列表
        List<QuestionBank> list_bank =  questionBankBiz.list_bank();
        //默认设置


        Map<String,Object> data = new HashMap<>(2);
        data.put("list_bank",list_bank);


        return ResponseUtil.ok(data);
    }


    @GetMapping("/save_bank")
    public Object save_bank(Integer userId,String resultList){


        logger.info(userId);
        logger.info(resultList);
        String[] list = resultList.split(",");

        BankForUser baseUser = new BankForUser();
        baseUser.setUserId(userId);
        List<BankForUser> listBankForUser = bankForUserBiz.selectList(baseUser);
        baseUser.setDeleted(1);
        if(listBankForUser.size() == 0) {
            for(int i = 0; i < list.length; i ++) {
                Integer bankId = Integer.parseInt(list[i]);
                BankForUser bankForUser = new BankForUser();
                bankForUser.setUserId(userId);
                bankForUser.setDeleted(0);
                bankForUser.setBankId(bankId);
                bankForUser.setAddTime(new Date());
                bankForUserBiz.insert(bankForUser);
            }
        }else {
            for(int k = 0 ;k < listBankForUser.size(); k ++ ) {
                listBankForUser.get(k).setDeleted(1);
                bankForUserBiz.updateById(listBankForUser.get(k));
            }
            for(int i = 0; i < list.length; i ++) {
                Integer bankId = Integer.parseInt(list[i]);
                int j = 0;
                for( ;j < listBankForUser.size(); j ++ ) {
                    if(bankId.equals(listBankForUser.get(j).getBankId())){
                        BankForUser bankForUser = listBankForUser.get(j);
                        bankForUser.setDeleted(0);
                        bankForUserBiz.updateById(bankForUser);
                        break;
                    }
                }
                if(j > listBankForUser.size()){
                    BankForUser bankForUser = new BankForUser();
                    bankForUser.setUserId(userId);
                    bankForUser.setDeleted(0);
                    bankForUser.setBankId(bankId);
                    bankForUser.setAddTime(new Date());
                    bankForUserBiz.insert(bankForUser);
                }
            }

        }

        return ResponseUtil.ok();
    }

}
